package com.augrain.microframework.trace.instrument.schedule;

import com.augrain.microframework.core.util.SysUtils;
import com.augrain.microframework.trace.TraceConstant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 定时任务线程池相关配置
 *
 * @author biaoy
 * @since 2023/08/13
 */
@ConditionalOnClass(ThreadPoolTaskExecutor.class)
public class ScheduleTaskConfiguration {

    @Bean(name = TraceConstant.THREAD_POOL_NAME)
    @ConditionalOnMissingBean
    public ThreadPoolTaskExecutor taskExecutor() {
        int cores = SysUtils.getCores();
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(cores);
        executor.setMaxPoolSize(cores * 2);
        executor.setQueueCapacity(1000);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("task-executor-");
        executor.setTaskDecorator(new CustomTaskDecorator());
        return executor;
    }
}
